package com.ruicar.afs.cloud.afscase.infomanagement.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.infomanagement.condition.CaseInfoQueryCondition;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseRedundantInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.*;
import com.ruicar.afs.cloud.afscase.infomanagement.vo.ApproveCostPageInfoVo;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinCostDetails;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinDiscountDetails;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinFinancingItems;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinRentAdjustDetails;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * Description:
 *
 * @author jiaqi.guo
 * @version 1.0
 * @date create on 2020-05-21 20:52
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api("融资信息")
@RequestMapping("/caseCostInfo")
public class CaseCostInfoController {

    private CaseCostInfoService caseCostInfoService;
    private CaseFinancingItemsService financingItemsService;
    private CaseDiscountDetailService discountDetailService;
    private CaseFinRentAdjustDetailsService rentAdjustDetailsService;
    private CaseRedundantInfoService redundantInfoService;


    /**
     * queryApproveCostPageInfo
     * <p>Description: 根据申请编号查询金融产品信息</p>
     *
     * @param
     * @return
     */
    @PostMapping(value = "/queryApproveCostPageInfo")
    @ApiOperation(value = "根据申请编号查询页面显示要素")
    public IResponse<ApproveCostPageInfoVo> queryApproveCostPageInfo(@ModelAttribute CaseInfoQueryCondition caseInfoQueryCondition) {

        return IResponse.success(caseCostInfoService.queryApproveCostPageInfo(caseInfoQueryCondition));
    }
    @PostMapping(value = "/resetCostInfo")
    @ApiOperation(value = "融资信息重置")
    public IResponse<Boolean> resetCostInfo(@RequestParam String applyNo) {
        CaseRedundantInfo redundantInfo= redundantInfoService.getOne(Wrappers.<CaseRedundantInfo>query().lambda()
                .eq(StringUtils.isNotBlank(applyNo),CaseRedundantInfo::getApplyNo,applyNo));
        if (ObjectUtils.isNotEmpty(redundantInfo)) {
            String approvalInfo = redundantInfo.getConditionalApproval();
            if (StringUtils.isNotBlank(approvalInfo)) {
                JSONObject json = JSONObject.parseObject(approvalInfo);
                //融资项
                JSONArray arrayFinancing = json.getJSONArray("finFinancingItemsList");
                if (ObjectUtils.isNotEmpty(arrayFinancing) && arrayFinancing.size() > 0) {
                    /**  解析融资信息FinFinancingItems*/
                    financingItemsService.deleteByApplyNo(applyNo);
                    ArrayList<FinFinancingItems> financingItemsList = new ArrayList<>();
                    arrayFinancing.toJavaList(FinFinancingItems.class).forEach(fin ->{
                        FinFinancingItems finFinancingItems = new FinFinancingItems();
                        BeanUtils.copyProperties(fin, finFinancingItems);
                        financingItemsList.add(finFinancingItems);
                        if(fin.getSonList()!=null&&fin.getSonList().size()>0){
                            fin.getSonList().forEach(son ->{
                                FinFinancingItems finFinancing = new FinFinancingItems();
                                BeanUtils.copyProperties(son, finFinancing);
                                financingItemsList.add(finFinancing);
                            });
                        }
                    });
                    financingItemsService.saveBatch(financingItemsList, financingItemsList.size());
                    log.info("取消变更融资项信息add成功！！！");
                }
                //租金调整
                JSONArray arrayRentAdjust = json.getJSONArray("finRentAdjustDetailsList");
                if (ObjectUtils.isNotEmpty(arrayRentAdjust) && arrayRentAdjust.size() > 0) {
                    /**  解析租金信息RentAdjustDetails*/
                    rentAdjustDetailsService.deleteByApplyNo(applyNo);
                    ArrayList<FinRentAdjustDetails> rentAdjustDetailsList = new ArrayList<>();
                    for (FinRentAdjustDetails adjustDetails : arrayRentAdjust.toJavaList(FinRentAdjustDetails.class)) {
                        FinRentAdjustDetails rentAdjustDetails = new FinRentAdjustDetails();
                        BeanUtils.copyProperties(adjustDetails, rentAdjustDetails);
                        rentAdjustDetailsList.add(rentAdjustDetails);
                    }
                    rentAdjustDetailsService.saveBatch(rentAdjustDetailsList, rentAdjustDetailsList.size());
                    log.info("取消变更租金add成功！！！");
                }
                //融资信息
                JSONArray costDetailsList = json.getJSONArray("finCostDetailsList");
                if (ObjectUtils.isNotEmpty(costDetailsList) && costDetailsList.size() > 0) {
                    /**  解析融资信息*/
                    caseCostInfoService.deleteByApplyNo(applyNo);
                    ArrayList<FinCostDetails> finCostDetailsList = new ArrayList<>();
                    for (FinCostDetails finCostDetails : costDetailsList.toJavaList(FinCostDetails.class)) {
                        FinCostDetails costDetails = new FinCostDetails();
                        BeanUtils.copyProperties(finCostDetails, costDetails);
                        finCostDetailsList.add(costDetails);
                    }
                    caseCostInfoService.saveBatch(finCostDetailsList, finCostDetailsList.size());
                    log.info("取消变更融资信息add成功！！！");
                }
                //贴息明细
                JSONArray discountList = json.getJSONArray("finDiscountDetailsList");
                if (ObjectUtils.isNotEmpty(discountList) && discountList.size() > 0) {
                    /**  解析贴息明细*/
                    discountDetailService.deleteByApplyNo(applyNo);
                    ArrayList<FinDiscountDetails> finDiscountDetailsList = new ArrayList<>();
                    for (FinDiscountDetails discountDetails : discountList.toJavaList(FinDiscountDetails.class)) {
                        FinDiscountDetails costDetails = new FinDiscountDetails();
                        BeanUtils.copyProperties(discountDetails, costDetails);
                        finDiscountDetailsList.add(costDetails);
                    }
                    discountDetailService.saveBatch(finDiscountDetailsList, finDiscountDetailsList.size());
                    log.info("取消变更贴息明细add成功！！！");
                }
            } else {
                throw new AfsBaseException("无原始数据可重置！！！");
            }
        } else {
            throw new AfsBaseException("无本案件详情信息！！！");
        }
        return new IResponse<Boolean>().setMsg("新增标签库成功");
    }
}